package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.activity.pojo.AdActivity;
import com.zhentao.model.announcement.dto.AnnouncementDto;
import com.zhentao.model.announcement.pojo.AdAnnouncement;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.AdAnnouncementService;
import com.zhentao.mapper.AdAnnouncementMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ad_announcement】的数据库操作Service实现
* @createDate 2024-07-05 08:47:55
*/
@Service
public class AdAnnouncementServiceImpl extends ServiceImpl<AdAnnouncementMapper, AdAnnouncement>
    implements AdAnnouncementService{
    @Resource
    AdAnnouncementMapper announcementMapper;

    @Override
    public ResponseResult getList(AnnouncementDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<AdAnnouncement> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<AdAnnouncement> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(dto.getContent()!=null,AdAnnouncement::getContent,dto.getContent());
        page = announcementMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    @Override
    public ResponseResult appList() {

        AdAnnouncement adAnnouncement = announcementMapper.selectNewOne();

        return ResponseResult.okResult(adAnnouncement);
    }

    @Override
    public ResponseResult addAnnouncement(AdAnnouncement announcement) {
        if (announcement==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        announcement.setIsDeleted(0);
        announcementMapper.insert(announcement);
        return ResponseResult.okResult("公告添加成功");
    }

    @Override
    public ResponseResult updateAnnouncement(AdAnnouncement announcement) {
        if (announcement==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        announcementMapper.updateById(announcement);
        return ResponseResult.okResult("公告修改成功");
    }

    @Override
    public ResponseResult putway(int id) {
        AdAnnouncement adAnnouncement = announcementMapper.selectById(id);
        if (adAnnouncement==null){
            return ResponseResult.errorResult(400,"未找到该活动");
        }
        adAnnouncement.setIsDeleted(adAnnouncement.getIsDeleted()==0 ? 1:0);
        if (adAnnouncement.getIsDeleted()==1){
            adAnnouncement.setUpdatedTime(new Date());
        }
        announcementMapper.updateById(adAnnouncement);
        return ResponseResult.okResult("状态修改成功");
    }

}




